package logic

import (
	"context"
	"fmt"
	"gozeroProject/model"

	"gozeroProject/rpc/internal/svc"
	"gozeroProject/rpc/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchUserLogic {
	return &SearchUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchUserLogic) SearchUser(in *user.SearchUserReq) (*user.SearchUserResp, error) {
	// 根据请求参数查询用户

	users, err := model.FindByUserIdOrName(in.UserId, in.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to search user: %w", err)
	}

	response := &user.SearchUserResp{
		List: make([]*user.UserReply, len(users)),
	}

	// 将查询结果映射到 UserReply 结构体
	for i, User := range users {
		response.List[i] = &user.UserReply{
			ID:       int32(User.Id),
			UserId:   User.UserId,
			Name:     User.Name,
			Mobile:   User.Mobile,
			Gender:   User.Gender,
			Nickname: User.Nickname,
			Type:     int32(User.Type),
		}
	}

	return response, nil
}
